time

  • 时间相关
  • 提供获取系统时间并格式化输出的功能
  • 提供系统级精确计时功能,用于程序性能分析
  • 计时起点:1970年1月1日0时0分0秒,可以用time.gmtime(0)获得
  • UTC时间:Coordinated Universal Time,世界标准时间,与GMT一致
  • DST,Daylight Saving Time,夏令时时间,源于系统底层C函数

时间的表示方法

  • 数字表示法

    一般从计时起点开始计算,如1548905599.0935512

    1
    2
    3
    time.time()
    # 1548906515.095716 本地当前时间戳
    time.time_ns()
  • struct_time时间表示法

    便于程序员使用的带有属性标签的时间描述对象,如 time.struct_time(tm_year=2019, tm_mon=1, tm_mday=31, tm_hour=3, tm_min=34, tm_sec=46, tm_wday=3, tm_yday=31, tm_isdst=0)

属性名 含义 取值范围
tm_year 四位整数,如2019
tm_mon [1,12]
tm_mday [1,31]
tm_hour 小时 [0,23]
tm_min 分钟 [0,59]
tm_sec [0,61] 60仅用于调时
tm_wday 星期 [0,6],星期一为0
tm_yday 全年第几天 [1,366]
tm_isdst 是否DST时间 (0, 1, -1)
0:DST
1:非DST
-1:随系统
tm_zone 时区的缩写
tm_gmtoff 与UTC时间相差的秒数
1
2
3
4
5
now = time.gmtime()
#time.struct_time(tm_year=2019, tm_mon=1, tm_mday=31, tm_hour=3, tm_min=52, tm_sec=31, tm_wday=3, tm_yday=31, tm_isdst=0)
now.tm_zone # UTC
now.tm_year # 2019
now[2] # 31
  • 字符串时间表示法

    便于用户查看的时间,如Thu Jan 31 11:36:28 2019

    1
    2
    time.ctime()
    #'Thu Jan 31 11:48:04 2019' 本地当前时间

库变量详解(4个)

变量/属性/类 含义
altzone 不知道
daylight 不知道
timezone 不知道
tzname 不知道

库函数详解(20个)

  • 时间获取(7个):time()、time_ns()、gmtime()、ctime()、asctime()、localtime()、mktime()
  • 时间格式化(2个):strftime()、strptime()
  • 程序计时(10个):clock()、monotonic()、monotonic_ns()、perf_counter()、perf_counter_ns()、process_time()、process_time_ns()、thread_time()、thread_time_ns()、sleep()
  • 辅助函数(1个):get_clock_info()

时间获取与格式化

时间获取与格式化函数 描述
time() 返回一个从计时起点开始到现在的秒数(浮点数)
time_ns() 返回一个从计时起点开始到现在的纳秒数(整数)
gmtime([seconds]) 返回表示当前UTC时间的struct_time对象,如果传入参数seconds,则把seconds转换成该时刻的struct_time
localtime([seconds]) 返回表示当前本地时间的struct_time对象,如果传入参数seconds,则把seconds转换成该时刻的struct_time
ctime([seconds]) 返回一个表示当前时间的字符串,如果传入参数seconds,则把seconds转换成该时刻的时间字符串
asctime([tuple]) 返回一个表示当前时间的字符串,如果传入参数时间元组,则把seconds转换成该时刻的时间字符串
mktime(st) 将一个struct_time对象转换成一个秒数浮点数
strftime() 按照给定格式化模板字符串,将struct_time转换成字符串
strptime() 按照给定格式化模板字符串,将字符串解析成struct_time对象

时间的格式化控制符

格式化控制符 说明 实例
%Y,%m,%d 年月日的数字形式 2019,01,31
%B,%b 月份的全写和缩写 January,Jan
%A,%a 星期的全写和缩写 Monday,Mon
%H,%M,%S 小时,分钟,秒 15,06,12
%I,%p 小时(12小时值),上下午 03,PM
%z 与UTC的偏移量 +0800
%c 区域设置适当的日期和时间表示形式。 Thu Jan 31 15:07:20 2019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
graph TD
num((整数/浮点数表示))
str((字符串表示))
struct_time((struct_time对象表示))

12[time,time_ns]-->num
34[gmtime,localtime]-->struct_time
56[ctime,asctime]-->str


struct_time==mktime==>num
num===34
num===56
struct_time==strftime==>str
str==strptime==>struct_time

程序计时

计时函数 描述
clock() 返回自从进程启动后的CPU时间或真实时间,调用之差是时间隔时间,不同平台精度不同。python3.3中被弃用,python3.8中移除。
monotonic(),monotonic_ns() 返回一个计时时间,两次调用之差是间隔时间,单调不可回退
perf_counter(),perf_counter_ns() 用于基准测试的性能计数器。返回一个精确计时时间,包含全部时间
process_time(),process_time_ns() 返回一个进程计时时间,不包含sleep时间
thread_time(),thread_time_ns() 用户态和内核态的CPU时间之和
sleep(seconds) 将线程挂起seconds秒,seconds可以是浮点数

辅助函数

辅助函数 描述
get_clock_info(name) 返回以下六种计时器的属性值
‘clock’,’monotonic’,’perf_counter’,’process_time’,’thread_time’,’time’
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import time

timers = ['clock','monotonic','perf_counter','process_time','thread_time','time']
for timer in timers:
print(timer,time.get_clock_info(timer),end='\n\n')

'''
Warning (from warnings module):
File "C:/Users/xiaoy/Documents/t.py", line 6
print(timer,time.get_clock_info(timer),end='\n\n')
DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
clock namespace(adjustable=False, implementation='QueryPerformanceCounter()', monotonic=True, resolution=1e-07)

monotonic namespace(adjustable=False, implementation='GetTickCount64()', monotonic=True, resolution=0.015625)

perf_counter namespace(adjustable=False, implementation='QueryPerformanceCounter()', monotonic=True, resolution=1e-07)

process_time namespace(adjustable=False, implementation='GetProcessTimes()', monotonic=True, resolution=1e-07)

thread_time namespace(adjustable=False, implementation='GetThreadTimes()', monotonic=True, resolution=1e-07)

time namespace(adjustable=True, implementation='GetSystemTimeAsFileTime()', monotonic=False, resolution=0.015625)
'''
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
>>>import time
>>>s = time.time()
>>>s
1534551957.9329424
>>>time.ctime(s)
'Sat Aug 18 08:25:57 2018'
>>>time.gmtime(s)
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=18, tm_hour=0, tm_min=25, tm_sec=57, tm_wday=5, tm_yday=230, tm_isdst=0)
>>>time.localtime(s)
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=18, tm_hour=8, tm_min=25, tm_sec=57, tm_wday=5, tm_yday=230, tm_isdst=0)
>>>t = time.gmtime()
>>>t
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=18, tm_hour=0, tm_min=25, tm_sec=57, tm_wday=5, tm_yday=230, tm_isdst=0)
>>>time.mktime(t)
1534523157.0
>>>time.asctime(t)
'Sat Aug 18 00:25:57 2018'
>>>time.strftime("%Y-%m-%d", t)
'2018-08-18'
>>>time.strftime("%H:%M:%S", t)
'00:25:57'
>>>time.strftime("%b %a %p", t)
'Aug Sat AM'
>>>t1 = time.strptime("2018-10-10", "%Y-%m-%d")
>>>t1
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=10, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=283, tm_isdst=-1)
>>>t2 = time.strptime("12:01:01","%H:%M:%S")
>>>t2
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=12, tm_min=1, tm_sec=1, tm_wday=0, tm_yday=1, tm_isdst=-1)
>>>t3 = time.strptime("2017=09=10","%Y=%m=%d")
>>>t3
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=10, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=253, tm_isdst=-1)
>>>t3 - t1
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
t3 - t1
TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'
>>>time.mktime(t1) - time.mktime(t3)
34128000.0
>>>start = time.perf_counter()
>>>start
5.68888727071651e-07
>>>time.sleep(3)
>>>end = time.perf_counter()
>>>end-start
19.309388143107373